TEGetOffset
TEGetOffset Obtain a character offset associated with a screen point
#include <TextEdit.h> TextEdit
short TEGetOffset(thePt, hTE );
Point thePt; in local coordinates; usually a mouseDown Point
TEHandle hTE; handle of an edit record
returns an offset within the data at TERec.hText
TEGetOffset returns an offset which you can use to identify the character
associated with a point on the screen. This works with both types of
edit record.
thePt is the point, in local coordinates. It is usually a value obtained from
the where field of the EventRecord structure (and converted to local
coordinates via GlobalToLocal).
hTE is a handle obtained via TENew (old style TextEdit record) or
TEStylNew (new style TextEdit record). It leads to a
variable-length TERec structure and identifies the edit record of
interest.
Returns: a short; an offset within the edit record's text of the character that
is currently drawn at thePt.

Notes: TEGetOffset lets you look up the text associated with a
mouseDown. Thus, you can "micro-manage" the functions usually performed
by TEClick or TEKey, et al. The following code uses this offset to replace
the character at a mouse click:
short theOffset;
CharsHandle theChars;
char *cp;
GlobalToLocal( &evp->where ); /* local coordinates */
HLock( hTE);
theOffset = TEGetOffset( evp->where, hTE );
theChars = TEGetText(hTE);
cp = *theChars;
cp[theOffset]='X';
HUnlock( hTE);
InvalRect( &(*hTE)->viewRect ); /* way to force redraw */
TEGetOffset works for old-format as well as new-format edit records.
Use TEGetPoint for the inverse operation (locate a screen point
associated with a particular character).